<template>
    <el-container>
        <el-aside width="200px">
            <el-menu
            @select="SelectMenuListener">
                <el-submenu index="1">
                    <template slot="title">
                        <i class="el-icon-setting"></i>
                        <span>操作</span>
                    </template>
                    <el-menu-item-group>
                        <template slot="title">日志项目操作</template>
                        <el-menu-item index="add">新增项目</el-menu-item>
                        <el-menu-item index="home">返回主页</el-menu-item>
                    </el-menu-item-group>
                </el-submenu>
            </el-menu>
            <el-menu
                @select="SelLogMenuLsner"
                v-for="item in items">                       
                <el-submenu :index="item.id">
                    <template slot="title">
                        <el-badge class="mark" :value="item.count" />
                        <span>{{item.name}}</span>
                    </template>
                    <el-menu-item-group>
                        <el-menu-item index="view"><i class="el-icon-view"></i> 查看</el-menu-item>
                        <el-menu-item index="edit"><i class="el-icon-edit"></i> 编辑</el-menu-item>
                        <!-- 没有子记录时可以删除日志项 -->
                        <el-menu-item index="del" v-if="item.count < 1"><i class="el-icon-circle-close"></i>删除</el-menu-item>
                    </el-menu-item-group>
                </el-submenu>
            </el-menu>
        </el-aside>
        <!-- 内置路由 -->
        <!-- 组件传值 -->
        <log-edit v-if="centerRouter.path == 'edit'" :center-router="centerRouter"></log-edit>       
        <log-home v-else :mx="HomeArgs"></log-home>
    </el-container>
</template>

<script>
import Main from './main.js'
import LogEdit from './edit.vue'
import LogHome from './home.vue'
import {Util} from '../../Util'
export default {
    data() {
        var hash = location.hash,
            queryIdx
        ;
        if(hash && (queryIdx = hash.indexOf('?')) > -1){
            hash = hash.substr(1, queryIdx-1);
        }else if(hash){
            hash = hash.substr(1);
        }
        var defRputer = hash? hash:'home';
        var id = this.$c.getQuery('index');
        var bind = {};
        if(id){
            bind = {id, name:'编辑数据'};
        }
        return {
            items: [],
            // 路由地址
            centerRouter: {
                path: defRputer,
                bind
            },
            HomeArgs: {}
        }
    },
    // 创建示例以后
    created () {
        Main.getList(this, 1, (rs)=>{            
            if('home' == this.centerRouter.path && this.centerRouter.bind.id){
                var args = Util.mapQueByKey(rs, 'id', this.centerRouter.bind.id)
                if(args){
                    this.HomeArgs = args
                }
            }
        })
    },
    // 方法集
    methods: {
        // 选中菜单事件处理
        SelectMenuListener(index, indexPath){
            // 编辑页面
            if('add' == index){
                this.centerRouter = {
                    path: 'edit',
                    bind: {name:'新增数据'}
                }
                location.href = '#edit'
            }else if('home' == index){
                this.centerRouter = {
                    path: 'home',
                    bind: {}
                }
                location.href = '#home'
            }
        },
        // 日志列表菜单监听
        SelLogMenuLsner(index, indexPath){
            // 编辑页面
            var id = indexPath[0];
            if('edit' == index){
                this.centerRouter = {
                    path: 'edit',
                    bind: {id,name:'编辑数据'}           // 路由绑定值
                }
                // 挂起绑定值
                location.href = '#edit?index='+id
            }
            else if('view' == index){
                this.centerRouter = {
                    path: 'home',
                    bind: {id}           // 路由绑定值
                }
                // 挂起绑定值
                location.href = '#home?index='+id
                var args = Util.mapQueByKey(this.items, 'id', id)
                if(args){
                    this.HomeArgs = args
                }
            }
            else if('del' == index){
                const loading = this.$loading({
                    lock: true,
                    text: 'Loading',
                    spinner: 'el-icon-loading',
                    background: 'rgba(0, 0, 0, 0.7)'
                })
                this.$c.PigeonsGet('aurora/ulog/del', 
                    {id}, 
                    (result)=>{
                        if(result.code == 200){  
                            this.$message({message: result.msg, type: 'success'})
                            setTimeout(function(){
                                location.reload() 
                            },2000)
                        }
                    },
                    (msg) => {
                        this.$message({message: msg, type: 'warning'})
                    },
                    () => {
                        loading.close()
                    }
                )
            }
        }
    },
    // 组件
    components:{
        LogEdit,
        LogHome
    }
}
</script>

<style>
    /* .el-header,  */
    .el-footer {
      background-color: #B3C0D1;
      color: #333;
      text-align: center;
      line-height: 60px;
    }
    
    .el-aside {
      background-color: #D3DCE6;
      color: #333;
      text-align: center;
      line-height: 200px;
    }
    
    .el-main {
      background-color: #E9EEF3;
      color: #333;
      text-align: center;
      /* line-height: 160px; */
    }

    body > .el-container {
    margin-bottom: 40px;
    min-height: 700px;
  }
  
  .el-container:nth-child(5) .el-aside,
  .el-container:nth-child(6) .el-aside {
    line-height: 260px;
  }
  
  .el-container:nth-child(7) .el-aside {
    line-height: 320px;
  }

</style>